package com.itheima.controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.pagehelper.PageInfo;
import com.itheima.domain.Customer;
import com.itheima.domain.LinkMan;
import com.itheima.domain.UserInfo;
import com.itheima.domain.Visit;
import com.itheima.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.security.RolesAllowed;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * ClassName:VisitController
 * Package:com.itheima.controller
 * Description:
 * Date:2020/3/24 14:21
 * User: snow
 */
@Controller
@RequestMapping("/visit")
public class VisitController {

	@Autowired
	private VisitService visitService;
	@Autowired
	private CustomerService customerService;
	@Autowired
	private LinkManService linkManService;
	@Autowired
	private UserInfoService userInfoService;


	@RequestMapping("/findAll.do")
	public ModelAndView findAll
			(@RequestParam(name = "page",required = true,defaultValue = "1")Integer page,
	                            @RequestParam(name = "size", required = true,defaultValue = "14")Integer size) throws Exception {
		ModelAndView view = new ModelAndView();
		List<Customer> customerList = customerService.findAll();
		view.addObject("customerList",customerList);
		List<LinkMan> linkManList = linkManService.findAll();
		view.addObject("linkManList",linkManList);
		List<UserInfo> visitInfoList = userInfoService.findAll();
		view.addObject("visitInfoList",visitInfoList);
		List<Visit> visitList = visitService.findAll(page,size);
		// PageInfo是一个分页bean
		PageInfo pageInfo = new PageInfo(visitList);
		view.addObject("pageInfo",pageInfo);
		view.setViewName("visit/visit-list");
		return view;
	}

	/**
	 * 模糊查询
	 * @param page
	 * @param size
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/findSome.do")
	public ModelAndView findSome
			(@RequestParam(name = "page",required = true,defaultValue = "1")Integer page,
			 @RequestParam(name = "size", required = true,defaultValue = "14")Integer size,Visit visit) throws Exception {
		ModelAndView view = new ModelAndView();
		// 遍历，获取所有数据
		List<Customer> customerList = customerService.findAll();
		List<LinkMan> linkManList = linkManService.findAll();
		List<UserInfo> visitInfoList = userInfoService.findAll();
		view.addObject("customerList",customerList);
		view.addObject("linkManList",linkManList);
		view.addObject("visitInfoList",visitInfoList);
		// 返回查询的数据
		view.addObject("visit",visit);
		//获取拜访记录
		List<Visit> visitList = visitService.findAll(page,size,visit);
		// PageInfo是一个分页bean
		PageInfo pageInfo = new PageInfo(visitList);
		view.addObject("pageInfo",pageInfo);
		view.setViewName("visit/visit-list");
		return view;
	}

	/**
	 * 详情查询
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/findById.do")
	public ModelAndView findById(String id)throws Exception {
		ModelAndView view = new ModelAndView();
		// 获取拜访信息表
		Visit visit = visitService.findById(id);
		view.addObject("visit",visit);
		view.setViewName("visit/visit-show");
		return view;
	}



	/**
	 * 添加客户拜访记录前查找数据
	 *
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/saveBefore.do")
	public ModelAndView saveBefore() throws Exception {
		ModelAndView view = new ModelAndView();
		List<Customer> customerList = customerService.findAll();
		view.addObject("customerList", customerList);
		view.setViewName("visit/visit-add");
		return view;
	}

	/**
	 * 动态查询客户所属联系人 相应json
	 *          和  负责人
	 * @param
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/selectLinkMan.do")
	@ResponseBody
	public List<LinkMan> selectLinkMan(String customerId) throws Exception {
		// 1、获得ajax请求
		//2、获取联系人集合
		List<LinkMan> linkManList = linkManService.findLinkManByCustomerId(customerId);
		return linkManList;
	}


	/**
	 * 添加客户拜访记录
	 *
	 * @param visit
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/save.do")
	public String save(Visit visit) throws Exception {
		visitService.save(visit);
		return "redirect:findAll.do";
	}

	@RequestMapping("/delete.do")
	@RolesAllowed("ADMIN")
	public String delete(String id) throws Exception {
		visitService.delete(id);
		return "redirect:findAll.do";
	}


}
